﻿namespace YidanSoft.Library.EditorUtility.WinControls
{
    using System;
    using System.Diagnostics;
    using System.Drawing;
    using System.Windows.Forms;
    using YidanSoft.Library.EditorUtility.General;

    public class ButtonEx : Button
    {
        private bool gotFocus = false;
        private bool mouseDown = false;
        private bool mouseEnter = false;

        public ButtonEx()
        {
            base.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
        }

        protected void DrawBackground(Graphics g, DrawState state)
        {
            Rectangle clientRectangle = base.ClientRectangle;
            if ((state == DrawState.Normal) || (state == DrawState.Disable))
            {
                SolidBrush brush;
                g.FillRectangle(new SolidBrush(SystemColors.Control), clientRectangle);
                if (state == DrawState.Disable)
                {
                    brush = new SolidBrush(SystemColors.ControlDark);
                }
                else
                {
                    brush = new SolidBrush(SystemColors.ControlDarkDark);
                }
                g.DrawRectangle(new Pen(brush), clientRectangle.Left, clientRectangle.Top, clientRectangle.Width - 1, clientRectangle.Height - 1);
            }
            else if ((state == DrawState.Hot) || (state == DrawState.Pressed))
            {
                if (state == DrawState.Hot)
                {
                    g.FillRectangle(new SolidBrush(ColorUtil.VSNetSelectionColor), clientRectangle);
                }
                else
                {
                    g.FillRectangle(new SolidBrush(ColorUtil.VSNetPressedColor), clientRectangle);
                }
                g.DrawRectangle(SystemPens.Highlight, clientRectangle.Left, clientRectangle.Top, clientRectangle.Width - 1, clientRectangle.Height - 1);
            }
        }

        protected void DrawButtonState(Graphics g, DrawState state)
        {
            int num3;
            int num4;
            this.DrawBackground(g, state);
            Rectangle clientRectangle = base.ClientRectangle;
            bool flag = false;
            bool flag2 = base.Image != null;
            Size size = new Size(0, 0);
            if ((this.Text != string.Empty) && (this.Text != ""))
            {
                flag = true;
                size = TextUtil.GetTextSize(g, this.Text, this.Font);
            }
            int width = 0;
            int height = 0;
            if (flag2)
            {
                SizeF physicalDimension = base.Image.PhysicalDimension;
                width = (int) physicalDimension.Width;
                height = (int) physicalDimension.Height;
                if ((width > clientRectangle.Width) || (height > clientRectangle.Height))
                {
                    Debug.WriteLine("Image dimensions need to be smaller that button's dimension...");
                    return;
                }
            }
            if (!(!flag || flag2))
            {
                num3 = (base.Width - size.Width) / 2;
                num4 = (base.Height - size.Height) / 2;
                this.DrawText(g, this.Text, state, num3, num4);
            }
            else if (!(!flag2 || flag))
            {
                num3 = (base.Width - width) / 2;
                num4 = (base.Height - height) / 2;
                this.DrawImage(g, state, base.Image, num3, num4);
            }
            else
            {
                num3 = (((base.Width - size.Width) - width) - 2) / 2;
                num4 = (base.Height - height) / 2;
                this.DrawImage(g, state, base.Image, num3, num4);
                num3 += width + 2;
                num4 = (base.Height - size.Height) / 2;
                this.DrawText(g, this.Text, state, num3, num4);
            }
        }

        protected void DrawImage(Graphics g, DrawState state, Image image, int x, int y)
        {
            SizeF physicalDimension = base.Image.PhysicalDimension;
            int width = (int) physicalDimension.Width;
            int height = (int) physicalDimension.Height;
            if (state == DrawState.Normal)
            {
                g.DrawImage(base.Image, x, y, width, height);
            }
            else if (state == DrawState.Disable)
            {
                ControlPaint.DrawImageDisabled(g, base.Image, x, y, SystemColors.Control);
            }
            else if ((state == DrawState.Pressed) || (state == DrawState.Hot))
            {
                ControlPaint.DrawImageDisabled(g, base.Image, x + 1, y, SystemColors.Control);
                g.DrawImage(base.Image, x, y - 1, width, height);
            }
        }

        protected void DrawText(Graphics g, string Text, DrawState state, int x, int y)
        {
            if (state == DrawState.Disable)
            {
                g.DrawString(Text, this.Font, SystemBrushes.ControlDark, (PointF) new Point(x, y));
            }
            else
            {
                g.DrawString(Text, this.Font, SystemBrushes.ControlText, (PointF) new Point(x, y));
            }
        }

        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);
            this.gotFocus = true;
            base.Invalidate();
        }

        protected override void OnLostFocus(EventArgs e)
        {
            base.OnLostFocus(e);
            this.gotFocus = false;
            base.Invalidate();
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            this.mouseDown = true;
            base.Invalidate();
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            this.mouseEnter = true;
            base.OnMouseEnter(e);
            base.Invalidate();
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            this.mouseEnter = false;
            base.OnMouseLeave(e);
            base.Invalidate();
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            this.mouseDown = false;
            base.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            Graphics g = pe.Graphics;
            if (this.mouseDown)
            {
                this.DrawButtonState(g, DrawState.Pressed);
            }
            else if (this.gotFocus || this.mouseEnter)
            {
                this.DrawButtonState(g, DrawState.Hot);
            }
            else if (base.Enabled)
            {
                this.DrawButtonState(pe.Graphics, DrawState.Normal);
            }
            else
            {
                this.DrawButtonState(pe.Graphics, DrawState.Disable);
            }
        }
    }
}

